#ifndef QBOT_MASTER_H_
#define QBOT_MASTER_H_

#include <string.h>
#include "simple_dataframe.h"
#include "data_holder.h"
#include "serialport.h"
#include <boost/thread.hpp>
#include <boost/thread/lock_factories.hpp>

typedef struct
{
    struct
    {
        double voltage;
        double current;
        double RSOC;
    } BMS;
    bool DI[6];
    bool DO[6];
} Battery_T;

class QbotMaster : public Dataframe{
    public:
        QbotMaster(SerialPort* trans=0);
        ~QbotMaster();
        void register_notify(const MESSAGE_ID id, Notify* _nf){
        }

        bool data_recv(unsigned char c);
        bool data_parse();
        bool init();
        bool is_alive(){return alive;}
        bool interact(const MESSAGE_ID id);
        bool send_message(const MESSAGE_ID id);
        bool send_message(const MESSAGE_ID id, unsigned char* data, unsigned char len);
        bool send_message(Message* msg);
        bool set_speed(double vel){
            DataHolder::get()->velocity.v_liner_x = (short)(vel*1000.0);
            return interact(ID_SET_VELOCITY);
        }
        short get_speed()
        {
            return DataHolder::get()->velocity.v_liner_x;
        }
        RobotState_T getState()
        {
            return DataHolder::get()->robot_state;
        }
        bool set_power(bool onoff){
            DataHolder::get()->power_onoff = onoff;
            return interact(ID_SET_POWER);
        }

    private:
        bool recv_proc();
        void msg_sniff();
        void recv_task();
        void loop();

    public:
        Battery_T battery_info;

    private:
        bool alive;
        Message active_rx_msg;
        RECEIVE_STATE recv_state;
        SerialPort* trans;
        boost::shared_ptr<boost::thread> recv_thread;

};
#endif